package com.jhy.guavacase.utilites;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

/**
 * @author jhy
 * @ClassName SplitterCase.java
 * @Description Guava拆分器Splitter
 * @createTime 2022年01月27日 15:22:00
 */
public class SplitterCase {

    //1.基本示例-->按单个字符拆分
    @Test
    public void splitQuickStart(){
        List<String> res = Splitter.on("-").splitToList("java-spring-kafka");
        Assertions.assertEquals(res.size(),3);
        Assertions.assertEquals(res.get(0),"java");
        Assertions.assertEquals(res.get(1),"spring");
        Assertions.assertEquals(res.get(2),"kafka");
    }

    //2.按字符串拆分
    @Test
    public void splitByString(){
        List<String> res = Splitter.on("---").splitToList("java---spring---kafka");
        Assertions.assertEquals(res.size(),3);
        Assertions.assertEquals(res.get(0),"java");
        Assertions.assertEquals(res.get(1),"spring");
        Assertions.assertEquals(res.get(2),"kafka");
    }

    //3.按正则表达式拆分
    @Test
    public void splitByPattern(){
        //List<String> res = Splitter.on(Pattern.compile("[1-9]")).splitToList("java1spring2kafka");
        List<String> res = Splitter.onPattern("[1-9]").splitToList("java1spring2kafka");
        Assertions.assertEquals(res.size(),3);
        Assertions.assertEquals(res.get(0),"java");
        Assertions.assertEquals(res.get(1),"spring");
        Assertions.assertEquals(res.get(2),"kafka");
    }

    //4.按固定长度拆分
    @Test
    public void splitByFixedLength(){
        List<String> res = Splitter.fixedLength(5).splitToList("java1spring2kafka");
        Assertions.assertEquals(res.size(),4);
        Assertions.assertEquals(res.get(0),"java1");
        Assertions.assertEquals(res.get(1),"sprin");
        Assertions.assertEquals(res.get(2),"g2kaf");
        Assertions.assertEquals(res.get(3),"ka");
    }

    //5.按字符匹配器CharMatcher拆分
    @Test
    public void splitByCharMatcher(){
        List<String> res = Splitter.on(CharMatcher.breakingWhitespace()).splitToList("java spring kafka");
        Assertions.assertEquals(res.size(),3);
        Assertions.assertEquals(res.get(0),"java");
        Assertions.assertEquals(res.get(1),"spring");
        Assertions.assertEquals(res.get(2),"kafka");
    }


    //6.自动忽略空白字符串
    @Test
    public void splitAndOmitEmpty(){
        List<String> res = Splitter.on("-").omitEmptyStrings().splitToList("java-spring--kafka");
        Assertions.assertEquals(res.size(),3);
        Assertions.assertEquals(res.get(0),"java");
        Assertions.assertEquals(res.get(1),"spring");
        Assertions.assertEquals(res.get(2),"kafka");
    }

    //7.移除结果字符串的前导空白和尾部空白
    @Test
    public void splitAndTrim(){
        List<String> res = Splitter.on("-").trimResults().splitToList(" java - spring-kafka ");
        Assertions.assertEquals(res.size(),3);
        Assertions.assertEquals(res.get(0),"java");
        Assertions.assertEquals(res.get(1),"spring");
        Assertions.assertEquals(res.get(2),"kafka");
    }

    //8.限制拆分出的字符串数量
    @Test
    public void splitAndLimit(){
        List<String> res = Splitter.on("-").limit(2).splitToList("java-spring-kafka");
        Assertions.assertEquals(res.size(),2);
        Assertions.assertEquals(res.get(0),"java");
        Assertions.assertEquals(res.get(1),"spring-kafka");
    }

    //9.拆分并转为map
    @Test
    public void splitAndToMap(){
        Map<String, String> res = Splitter.on("-").withKeyValueSeparator(":").split("java:语言-spring:框架-kafka:中间件");
        Assertions.assertEquals(res.size(),3);
        Assertions.assertEquals(res.get("java"),"语言");
        Assertions.assertEquals(res.get("spring"),"框架");
        Assertions.assertEquals(res.get("kafka"),"中间件");
    }




}
